using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public abstract class EffectLoadingController : ILoadingController
    {
        protected abstract string effectPath { get; }
        public uint MaxStore => 1;

        private LoadingEffectEntityView effectEntity;

        private bool enable = false;
        public void Start()
        {
            this.enable = true;
            this.PlayLoadingEffect().Forget();
        }

        private async UniTaskVoid PlayLoadingEffect()
        {
            if (this.effectEntity == null)
            {
                ManagerCenter.Entity.EnableGroup((int)EntityPoolGroupType.LoadingEffect);
                this.effectEntity = ManagerCenter.Entity.Get(this.effectPath, (int)EntityPoolGroupType.LoadingEffect) as LoadingEffectEntityView;
            }
            await this.effectEntity.Load();
            if (!this.enable)
            {
                this.effectEntity.Put();
                return;
            }

        }

        public void UpdateProgress(float ratio)
        {

        }

        public void End()
        {
            this.enable = false;
            if (this.effectEntity != null && this.effectEntity.isLoaded)
            {
                this.effectEntity.Put();
            }
            this.effectEntity = default;
        }

        public void OnReset()
        {
            End();
        }
    }

    public class LoadingEffectEntityView : Entity
    {
        public void Put()
        {
            this.EntityGroup?.HideEntity(this);
        }
    }
}
